package org.example.test;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.example.annotation.NavigationCases;
import org.example.annotation.Operation;

public class RunOperation {
	private Class clazz;

	@SuppressWarnings({ "rawtypes", "unchecked" })
	public RunOperation(Class clazz) {
		if (clazz.isAnnotationPresent(Operation.class)) {
			Operation op = (Operation) clazz.getAnnotation(Operation.class);

			System.out.println(op.value().name());
		} else {
			throw new IllegalArgumentException(clazz.getName());
		}
		
		this.clazz = clazz;
	}

	public void run(String methodName) {
		try {
			Object o = clazz.newInstance();
			
			Method m = clazz.getMethod(methodName);

			NavigationCases cases = (NavigationCases) m
					.getAnnotation(NavigationCases.class);
			
			System.out.println(cases.toString());
			
			m.invoke(o);				
			
		} catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
			System.out.println(e.getCause().getMessage());
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		RunOperation runMyOp = new RunOperation(MyOperation.class);
		
		runMyOp.run("insert");
		runMyOp.run("update");
		
		new RunOperation(MySingletonOperation.class);
		new RunOperation(NotMyOperation.class);
	}

}